package com.woniu.tms.manage.service.impl;

import com.woniu.tms.entity.Permission;
import com.woniu.tms.manage.service.PermissionService;
import com.woniu.tms.manage.utils.MyUtils;
import com.woniu.tms.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public List<Permission> list() {
        List<Permission> nodes = permissionMapper.selectByExample(null);
        System.out.println("nodes.size() = " + nodes.size());
        System.out.println("nodes = " + nodes);
        return MyUtils.getTree(nodes);
    }

    @Override
    public void delete(Integer pid) {
        permissionMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public void update(Integer pid, String name) {
        Permission permission = new Permission();
        permission.setPid(pid);
        permission.setName(name);
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public void add(Integer pid, String name) {
        Permission permission = new Permission();
        permission.setParentid(pid);
        permission.setName(name);
        permissionMapper.insertSelective(permission);
    }

}
